<script setup lang="ts">
import { reactive, ref } from 'vue'
import { message } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { userRegisterUsingPost, userSendCodeUsingPost, userVerifyCodeUsingPost } from '@/api/userController.ts'

const formState = reactive<API.UserForgetPasswordRequest>({
  userAccount: '',
  userEmail: '',
  userVerifyCode: '',
})

const router = useRouter()

const isButtonDisabled = ref<boolean>(false);
const countdown = ref<number>(60);
const buttonText = ref<string>('获取验证码');

const userSendCode = async () => {
  // 判断账号是否为空
  if (formState.userAccount === '') {
    message.error('请先输入账号')
    return
  }
  // 判断邮箱是否为空
  if (formState.userEmail === '') {
    message.error('请先输入邮箱')
    return
  }
  // 验证邮箱格式是否正确
  const reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
  if (!reg.test(formState.userEmail)) {
    message.error('请输入正确的邮箱格式')
  }
  const res = await userSendCodeUsingPost(formState)
  if (res.data.code === 0 && res.data.data) {
    message.success('验证码发送成功')
  } else {
    message.error('验证码发送失败，' + res.data.message)
  }
  isButtonDisabled.value = true;
  const timer = setInterval(() => {
    countdown.value -= 1;
    buttonText.value = `${countdown.value} 秒后重新获取`;
    if (countdown.value === 0) {
      clearInterval(timer);
      isButtonDisabled.value = false;
      countdown.value = 60;
      buttonText.value = '获取验证码';
    }
  }, 1000);
}



/**
 * 提交表单
 * @param values
 */
const handleSubmit = async (values: any) => {
  const res = await userVerifyCodeUsingPost(values)
  if (res.data.code === 0 && res.data.data) {
    message.success('验证成功')
    await router.push({
      path: `/user/modifyPassword/${formState.userAccount}`,
      replace: true,
    })
  } else {
    message.error('验证失败，' + res.data.message)
  }
}
</script>

<template>
  <div id="userForgetPasswordPage">
    <h2 class="title">华彩联图云 - 忘记密码</h2>
    <div class="desc">企业级智能协同云图库</div>
    <a-form
      :model="formState"
      name="basic"
      label-align="left"
      autocomplete="off"
      @finish="handleSubmit"
    >
      <a-form-item name="userAccount" :rules="[{ required: true, message: '请输入账号' }]">
        <a-input v-model:value="formState.userAccount" placeholder="请输入账号" allow-clear />
      </a-form-item>
      <a-form-item name="userEmail" :rules="[{ required: true, message: '请输入邮箱' }]">
        <a-input v-model:value="formState.userEmail" placeholder="请输入邮箱" allow-clear />
      </a-form-item>
      <a-form-item name="userVerifyCode" :rules="[{ required: true, message: '请输入验证码' }]">
        <a-input-group compact>
          <a-input v-model:value="formState.userVerifyCode" style="width: calc(100% - 200px)" allow-clear/>
          <a-button type="primary" :disabled="isButtonDisabled" @click="userSendCode">
            {{ buttonText }}
          </a-button>
        </a-input-group>
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%">下一步</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<style scoped>
#userForgetPasswordPage {
  max-width: 360px;
  margin: 0 auto;
}

.title {
  text-align: center;
  margin-bottom: 16px;
}

.desc {
  text-align: center;
  color: #bbb;
  margin-bottom: 16px;
}
</style>
